package cn.wenlihelp.service.impl;

import cn.wenlihelp.bean.Resources;
import cn.wenlihelp.mapper.ResourcesMapper;
import cn.wenlihelp.service.ResourcesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResourcesServiceImpl implements ResourcesService {
    @Resource
    private ResourcesMapper resourcesMapper;

    @Override
    public PageInfo<Resources> findAll(int page, int limit, Resources resources) {
        PageHelper.startPage(page, limit);
        List<Resources> resourcess = this.findAll(resources);
        return new PageInfo<>(resourcess);
    }

    @Override
    public List<Resources> findAll(Resources resources) {
        return resourcesMapper.findAll(resources);
    }

    @Override
    public List<Resources> findByRoleId(Integer roleId) { return resourcesMapper.findByRoleId(roleId);    }

    @Override
    public Resources findById(Long id) {
        return resourcesMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        try {
            resourcesMapper.deleteByIds(ids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean insert(Resources resources) {
        try {
            return resourcesMapper.insertSelective(resources) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateByPrimaryKey(Resources resources) {
        try {
            return resourcesMapper.updateByPrimaryKeySelective(resources) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
